package mn.idax.exchange.otc.task;

import com.alibaba.fastjson.JSON;
import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.simple.SimpleJob;
import com.dangdang.elasticjob.lite.annotation.ElasticSimpleJob;
import mn.idax.exchange.otc.domain.NeedAutoCommentDTO;
import mn.idax.exchange.otc.entity.OTCComment;
import mn.idax.exchange.otc.enums.CommentEnum;
import mn.idax.exchange.otc.enums.CommentTypeEnum;
import mn.idax.exchange.otc.enums.OrderEnum;
import mn.idax.exchange.otc.mapper.OTCCommentMapper;
import mn.idax.exchange.otc.service.OTCOrderService;
import mn.idax.exchange.otc.service.OTCUserService;
import mn.idax.exchange.otc.util.DateUtils;
import mn.idax.exchange.otc.util.LogUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;

/**
 * @Author: jack_huang
 * @Date: 2019/7/31 14:35
 * 默认好评（订单完成交易且7天） 每1小时 与实际有出入 除非1分钟跑一次，但没必要
 */
@ElasticSimpleJob(cron = "0 0 0/1 * * ?")
@Component
public class CommmentAutoJob implements SimpleJob {

    @Autowired
    private OTCOrderService otcOrderService;

    @Autowired
    private OTCCommentMapper otcCommentMapper;

    @Autowired
    private OTCUserService otcUserService;
    @Override
    public void execute(ShardingContext shardingContext) {

        //获取app 2.3上线后交易完成 但没评价的订单
        List<NeedAutoCommentDTO> needAutoCommentOrder = otcOrderService.getNeedAutoCommentOrder();
        if (!needAutoCommentOrder.isEmpty()){
            for (NeedAutoCommentDTO needAutoCommentDTO : needAutoCommentOrder) {

                try {
                    if (needAutoCommentDTO.getTotal().equals(1)){
                        //买家已评 帮卖家评买家
                        if (needAutoCommentDTO.getTradeType().equals(OrderEnum.TYPE_BUY.getCode())){
                            OTCComment otcComment = constructInsertRecord(needAutoCommentDTO.getNumber(), needAutoCommentDTO.getSeller(), needAutoCommentDTO.getBuyer(),
                                    needAutoCommentDTO.getAdId(), CommentEnum.COMMENT_BY_SELLER.getCode());
                            int insert = otcCommentMapper.insertSelective(otcComment);
                            if (insert > 0){
                                //更新好评率
                                otcUserService.updateUserGoodRate(needAutoCommentDTO.getBuyer());
                            }

                        } else {
                            OTCComment otcComment = constructInsertRecord(needAutoCommentDTO.getNumber(), needAutoCommentDTO.getBuyer(), needAutoCommentDTO.getSeller(),
                                    needAutoCommentDTO.getAdId(), CommentEnum.COMMENT_BY_BUYER.getCode());
                            int insert = otcCommentMapper.insertSelective(otcComment);
                            if (insert > 0){
                                //更新好评率
                                otcUserService.updateUserGoodRate(needAutoCommentDTO.getSeller());
                            }

                        }

                    } else {
                        //补两条数据
                        //买家评卖家
                        OTCComment buyerComment = constructInsertRecord(needAutoCommentDTO.getNumber(), needAutoCommentDTO.getBuyer(), needAutoCommentDTO.getSeller(),
                                needAutoCommentDTO.getAdId(), CommentEnum.COMMENT_BY_BUYER.getCode());
                        int buyerInsert = otcCommentMapper.insertSelective(buyerComment);
                        if (buyerInsert > 0){
                            //更新好评率
                            otcUserService.updateUserGoodRate(needAutoCommentDTO.getSeller());
                        }
                        //卖家评买家
                        OTCComment sellerComment = constructInsertRecord(needAutoCommentDTO.getNumber(), needAutoCommentDTO.getSeller(), needAutoCommentDTO.getBuyer(),
                                needAutoCommentDTO.getAdId(), CommentEnum.COMMENT_BY_SELLER.getCode());
                        int sellerInsert = otcCommentMapper.insertSelective(sellerComment);
                        if (sellerInsert > 0){
                            //更新好评率
                            otcUserService.updateUserGoodRate(needAutoCommentDTO.getBuyer());
                        }
                    }
                } catch (Exception e){
                    LogUtils.error(this.getClass(),"autoUpdateCommentOrder fail " + JSON.toJSONString(needAutoCommentDTO) + ExceptionUtils.getStackTrace(e));
                }

            }
        }
        LogUtils.info(this.getClass(),"autoUpdateCommentOrderFinish ******");
    }

    /**
     * 构造update 评价bean
     * @param number
     * @param commentUser
     * @param beCommentUser
     * @param adId
     * @param tradeType
     * @return
     */
    public OTCComment constructInsertRecord(String number,Integer commentUser,Integer beCommentUser,Integer adId,Integer tradeType){
        OTCComment otcComment = new OTCComment();
        otcComment.setOrdernum(number);
        otcComment.setCommentuserid(commentUser);
        otcComment.setBecommentuserid(beCommentUser);
        otcComment.setAdid(adId);
        otcComment.setCommenttype(CommentTypeEnum.COMMENT_GOOD.getCode());
        otcComment.setTradetype(tradeType);
        otcComment.setIscontent(false);
        otcComment.setIsdelete(false);
        otcComment.setCommenttime(DateUtils.getUTCNow());
        otcComment.setCreatetime(DateUtils.getUTCNow());
        otcComment.setUpdatetime(DateUtils.getUTCNow());

        return otcComment;
    }
}
